<?php
/**
 * Created by PhpStorm.
 * Author: huangwy
 * Date: 2018/4/3 15:23
 * Version: 1.0
 */

namespace app\admin\controller;


class Group extends Base
{
    public function index()
    {
        $group = db('auth_group')->select();
        $this->assign('list', $group);
        $this->assign('nav', array('user', 'grouplist', 'grouplist'));//导航
        return view();
    }

    public function del()
    {
        $ids = input('ids');
        $ids = isset($ids) ? $ids : false;

        if (!$ids) {
            return json(['flag'=>1,'message'=>'请勾选删除用户组!']);
        }

        if (is_array($ids)) {
            foreach ($ids as $k => $v) {
                $ids[$k] = intval($v);
            }
            $ids = implode(',', $ids);
        }

        if (db('auth_group')->where('id','in',$ids)->delete()) {
            addlog('删除用户组ID：' . $ids);
            return json(['flag'=>0,'message'=>'恭喜，用户组删除成功!']);
        } else {
            return json(['flag'=>0,'message'=>'参数错误']);
        }

    }

    public function update()
    {
        $title = input('title');
        $id = input('id');
        $data['title'] = isset($title) ? trim($title) : false;
        $id = isset($id) ? intval($id) : false;
        if ($data['title']) {
            $status = input(status);
            $status = isset($status) ? $status : '';
            if ($status == 'on') {
                $data['status'] = 1;
            } else {
                $data['status'] = 0;
            }
            //如果是超级管理员一直都是启用状态
            if ($id == 1) {
                $data['status'] = 1;
            }

            $rules = isset($_POST['rules']) ? $_POST['rules'] : 0;
            if (is_array($rules)) {
                foreach ($rules as $k => $v) {
                    $rules[$k] = intval($v);
                }
                $rules = implode(',', $rules);
            }
            $data['rules'] = $rules;
            if ($id) {
                $group = db('auth_group')->where('id=' . $id)->data($data)->update();
                if ($group) {
                    addlog('编辑用户组，ID：' . $id . '，组名：' . $data['title']);
                    return json(['flag'=>0,'message'=>'恭喜，用户组修改成功!']);
                    exit(0);
                } else {
                    return json(['flag'=>0,'message'=>'未修改内容!']);
                }
            } else {
                db('auth_group')->data($data)->insert();
                addlog('新增用户组，ID：' . $id . '，组名：' . $data['title']);
                return json(['flag'=>0,'message'=>'恭喜，用户组修改成功!']);
                exit(0);
            }
        } else {
            return json(['flag'=>1,'message'=>'用户组名称不能为空!']);
        }
    }

    public function edit()
    {
        $id = input('id');
        $id = isset($id) ? intval($id) : false;
        if (!$id) {
            $this->error('参数错误！');
        }

        $group = db('auth_group')->where('id=' . $id)->find();
        if (!$group) {
            $this->error('参数错误！');
        }
        //获取所有启用的规则
        $rule = db('auth_rule')->field('id,pid,title')->where('status=1')->order('o asc')->select();
        $group['rules'] = explode(',', $group['rules']);
        $rule = getMenu($rule);
        $this->assign('rule', $rule);
        $this->assign('group', $group);
        $this->assign('nav', array('user', 'grouplist', 'addgroup'));//导航
        return view('form');
    }

    public function add()
    {
        //获取所有启用的规则
        $rule = db('auth_rule')->field('id,pid,title')->where('status=1')->order('o asc')->select();
        $rule = getMenu($rule);
        $this->assign('rule', $rule);
        return view('form');
    }

    public function status()
    {

        $id = input('id');
        if (!$id) {
            return json(['flag'=>1,'message'=>'参数错误!']);
        }
        if ($id == 1) {
            return json(['flag'=>1,'message'=>'此用户组不可变更状态!']);
        }
        $group = db('auth_group')->where('id=' . $id)->find();
        if (!$group) {
            return json(['flag'=>1,'message'=>'参数错误!']);
        }
        $status = $group['status'];
        if ($status == 1) {
            $res = db('auth_group')->data(array('status' => 0))->where('id=' . $id)->update();
        }
        if ($status != 1 ) {
            $res = db('auth_group')->data(array('status' => 1))->where('id=' . $id)->update();
        }
        if ($res) {
            return json(['flag'=>0,'message'=>'恭喜，更新状态成功!']);
        } else {
            return json(['flag'=>1,'message'=>'更新失败!']);
        }
    }
}